/***
 * @author EX-FENGHE001
 */
;
(function(){
	'use strict';
		var app = angular.module('fwa.mainApp');
		app.controller('ConclusionApprovementListCtrl', ['$scope','$state','$modal','CaseAuditListService','BaseURL','Modal',
		                                     'FwaTabsetService','AuditDictServices','InsuranceService','CaseCommonService',
		                                function($scope,$state,$modal,caseAuditListService,
		                                		baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService,CaseCommonService){
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas();
		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
        var cellTemplate =  '<a ng-show="data.caseStatus == 6" ng-click="operation(data);">审批</a>';
		$scope.columns = [
		{
			label : '案件号',
			map : 'caseCode',
			width : '15%',
			 cellTemplate:'<a  ng:bind="data.caseCode" '+
                 'ng-click="queryAuditCase(data)"></a>'},
		{
			label : '机构名称',
			map : 'orgName',
			width : '20%'
		}, {
			label : '受理时间',
			map : 'auditDate',
			width : '15%'
		}, {
			label : '不合理费用',
			map : 'totalDeduction',
			width : '10%'
		}, {
			label : '告知时间',
			map : 'notifyDate',
			width : '10%'
		}, {
			label : '机构确认时间',
			map : 'affirmDate',
			width : '10%'
		}, {
			label : '案件状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : 'operation',
			width : '10%',
			cellTemplate:cellTemplate}];

		$scope.operation = function(data){
			$state.go('conclusionApprovement.caseAuditApprovement',{caseCode:data.caseCode,problemCode:data.relatedCode,urlFlag:''});
		}
		 $scope.queryAuditCase = function(data){
             $state.go("caseDetailView.detail",{caseCode:data.caseCode,problemCode:data.relatedCode});
         }
		$scope.onReset = function(){
            $scope.params.caseCode=null;
            $scope.params.orgName=null;
            $scope.params.auditDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	        };
        $scope.currentPage = 1;
        var start = 0;
        $scope.limit = 20;
        $scope.params = {
            page:0,
            pageSize:20
        };
            //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
            $scope.currentPage = v;
            start = $scope.limit*(v-1);
            $scope.onSubmit(true);
        });
        
	    $scope.onSubmit = function(resetPage){
            if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
	    	var fparam = $scope.params;
            if($scope.params.auditDate){
                var start = $scope.params.auditDate.start;
                var end = $scope.params.auditDate.end;
                fparam.beginDate = start;
                fparam.endDate = end;
            }
            if($scope.params.sbmitDate){
                var start = $scope.params.sbmitDate.start;
                var end = $scope.params.sbmitDate.end;
                fparam.start = start;
                fparam.end = end;
            }
            fparam.caseStatus="6";
	    	caseAuditListService.queryCaseAuditList(fparam).success(function(rst){
	    		if(rst){
            		$scope.tableData = rst.root;
        			$scope.totalCount = rst.total;
	    		}
	    	});
	    }
	    
	    $scope.onSubmitMore = function ()
	    {
	    	var array=$scope.checkRows;
	    	if($scope.checkRows.length <= 0){
	    		Modal.alert('请选择需要操作的选项！');
	    		return;
	    	} 
	    	var caseCodes = "";
	    	for(var i = 0;i<$scope.checkRows.length;i++){
	    		caseCodes+=$scope.checkRows[i].caseCode+",";
	    	}
	    	//弹出框。
	    	var scope = $scope.$new();
	    	var modalInstance=$modal.open({
				templateUrl:'conclusionApprovementSubmitMore_view.html',
				backdrop:'static',
				scope:scope,
				size:'sm',
				controller: function($scope, $modalInstance,Modal,$stateParams){
					$scope.auditDictServices = AuditDictServices;
					$scope.form = {};
					$scope.caseApproveResult ={};
					$scope.loading=false;
					//关闭弹窗
					$scope.onOk = function() {
						  $modalInstance.dismiss();
					}
					$scope.confirm=function(){
						  if(!$scope.caseApproveResult.opinion)
				           {
				               Modal.alert('请填写审批意见!');
				               return ;
				           } else if(!$scope.caseApproveResult.opinionDesc) {
				               Modal.alert('请填写意见说明！');
				               return ;
				           } 
						  Modal.confirm('确认批量操作吗？',function(){ 
						       	var opinionDTO = new Object();
						       	opinionDTO.opinion = $scope.caseApproveResult.opinion;
						       	opinionDTO.opinionDesc =$scope.caseApproveResult.opinionDesc;
						       	//从初审变更到待审核
						       	opinionDTO.statusAfter = 7;
						       	opinionDTO.statusCurrent = 6;
						       	//意见信息调查初审
						       	opinionDTO.opinionTypeCurrent = 15;
						       	opinionDTO.caseCodes=caseCodes;
						       	CaseCommonService.approvementAudits(opinionDTO).success(
									  function(data){
										  Modal.alert(data.msg);
										  $modalInstance.dismiss();
										  $scope.onSubmit();
									  });
						  });
					  }
				  }
				//controller end
			});
	    	//弹出框end
	    }
	    $scope.checkRows = [];
        function findCheck(d) {
            for(var i=0;i< $scope.checkRows.length;i++) {
                var checkedData =  $scope.checkRows [i];
                if(checkedData.caseCode == d.caseCode) {
                    return i;
                }
            }
            return -1;
        }

        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                $scope.checkRows.push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                $scope.checkRows.splice(index,1);
            }
        });
	    
	    
	    $scope.onSubmit();
	    
	// end line	
	}]);
})();
